package ac.il.technion.cs236369.hw5.core;

import java.sql.Date;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;

import ac.il.technion.cs236369.hw5.core.entities.Role;
import ac.il.technion.cs236369.hw5.core.entities.User;

/**
 * Application Lifecycle Listener implementation class ContextInitializer
 *
 */
public class ContextInitializer implements ServletContextListener {

	private Logger logger = Logger.getLogger(  ContextInitializer.class);
	private EntityManagerFactory emf = Persistence.createEntityManagerFactory( "jpaProvider");
    /**
     * Default constructor. 
     */
    public ContextInitializer() {
    }

	/**
     * @see ServletContextListener#contextInitialized(ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent arg0) {
        EntityManager manager = emf.createEntityManager( );
        EntityTransaction transaction = manager.getTransaction( );
        transaction.begin( ); 
        try {
        	Long count = manager.createQuery( "SELECT COUNT( r) FROM Role r", Long.class).getSingleResult( );
        	if ( count > 0) {
        		return;
        	}
        	Role admin = new Role( );
        	admin.setLevel( 1);
        	admin.setRole( "manager");
        	manager.persist( admin);
        	Role researcher = new Role( );
        	researcher.setLevel( 2);
        	researcher.setRole( "researcher");
        	manager.persist( researcher);
        	Role undefined = new Role( );
        	undefined.setLevel( 3);
        	undefined.setRole( "undefined");
        	manager.persist( undefined);
        	User sizzyyy = new User( );
        	sizzyyy.setActivityStatus( new Date( System.currentTimeMillis( ) + Long.valueOf( 365 * 24 * 60 * 60 * 1000)));
        	sizzyyy.setFirstName( "Artem");
        	sizzyyy.setLastName( "Barger");
        	sizzyyy.setLoginName( "bartem");
        	sizzyyy.setPermission( admin);
        	sizzyyy.setPhoneNumber( "0547588633");
        	sizzyyy.setPassword( "test");
        	manager.persist( sizzyyy);
        	User sniffer = new User( );
        	sniffer.setActivityStatus( new Date( System.currentTimeMillis( ) + Long.valueOf( 365 * 24 * 60 * 60 * 1000)));
        	sniffer.setFirstName( "Maxim");
        	sniffer.setLastName( "Aizenshtein");
        	sniffer.setLoginName( "sniffer");
        	sniffer.setPermission( admin);
        	sniffer.setPhoneNumber( "0507772499");
        	sniffer.setPassword( "test");
        	manager.persist( sniffer);
        	transaction.commit( );
        } catch( Exception e) {
        	logger.error( e.getMessage( ));
        	transaction.rollback( );
        } finally {
        	manager.close( );
        }
    }

	/**
     * @see ServletContextListener#contextDestroyed(ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent arg0) {
    	
    }
	
}
